package com.iweb.users.dao;

import com.iweb.users.model.User;
import com.iweb.util.JdbcUtil;

import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao{
    // 根据账号密码查询用户信息
    @Override
    public User queryByUsernamePassword(String account, String password) {
        User user = null;
        String sql = "select * from users where account = ? and password = ?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, account, password);
        if (!list.isEmpty()) {
            Map<String, Object> row = list.get(0);
            Integer id = (Integer) row.get("id");
            String username = (String) row.get("username");
            Integer user_attribute = (Integer) row.get("user_attribute");
            user = new User(id, username,account, password, user_attribute);
            return user;
        }else{
            return null;
        }
    }

    @Override
    public boolean isNull(String account) {
        String sql = "select * from users where account = ?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, account);
        boolean flag = false;
        if (!list.isEmpty()){
            return flag;
        }else{
            flag = true;
            return flag;
        }
    }
    @Override
    public void insertUser(User user) {
            String sql = "insert into users (account, password, username, user_attribute) values (null,?,?,?,?)";
            JdbcUtil.jdbcByUpdate(sql, user.getAccount(), user.getPassword(), user.getUsername(), user.getUser_attribute());
        }

}
